<?php
!defined('IN_FRAME') && die('404 Page');
class c_index extends Controller {
    
    public $arrManagerData = null;
    public $mdlAcl = null;
    
    public function __construct() {
        $mdlManager = Helper::loadModel('manager');
        if (!$mdlManager->logined()) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=login', '请先登录!');
        } else {
            $this->arrManagerData = $mdlManager->getSession();
            $this->mdlAcl = Helper::loadModel('acl');
        }
    }
    
    public function index() {
        $arrAssign = array(
            'strMsg' => '',
            'strOk' => '',
            'strTitle' => '后台首页',
            'arrCurManager' => $this->arrManagerData,
            'arrSys' => array(),
            'blnShowSys' => $this->mdlAcl->checkPermission($this->arrManagerData['group_name'], 'sys_config', 'view')
        );
        if ($arrAssign['blnShowSys']) {
            $mdlSys = new m_sys();
            $arrTemp = $mdlSys->select();
            $arrTemp && $arrAssign['arrSys'] = $arrTemp;
            if (count($_POST) > 0) {
                if (isset($_POST['add'])) {
                    if (!$this->mdlAcl->checkPermission($this->arrManagerData['group_name'], 'sys_config', 'add')) {
                        Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin', '你无权添加!');
                    }
                    $arrTemp = array(
                        'id' => isset($_POST['new_id']) ? $_POST['new_id'] : '',
                        'name' => isset($_POST['new_name']) ? $_POST['new_name'] : '',
                        'val' => isset($_POST['new_val']) ? $_POST['new_val'] : '',
                        'desed' => isset($_POST['new_desed']) ? 1 : 0,
                    );
                    $arrTemp['desed'] == 1 && $arrTemp['val'] = $mdlSys->getDesed($arrTemp['val']);
                    $bln = true;
                    if (empty($arrTemp['id'])) {
                        $bln = false;
                        $arrAssign['strMsg'] = 'KEY不能为空!';
                    }
                    if ($bln && !$mdlSys->isKey($arrTemp['id'])) {
                        $bln = false;
                        $arrAssign['strMsg'] = 'KEY由数字,大小写字母,下划线组成!';
                    }
                    if ($bln && false !== $mdlSys->get($arrTemp['id'])) {
                        $bln = false;
                        $arrAssign['strMsg'] = 'KEY已存在!';
                    }
                    if ($bln) {
                        if ($mdlSys->insert($arrTemp) === 0) {
                            $arrAssign['strOk'] = '添加成功!';
                            $arrAssign['arrSys'] = $mdlSys->select();
                        } else {
                            $arrAssign['strMsg'] = '未能完成添加,请检查!';
                        }
                    }
                } else {
                    $arrPostKey = array_keys($_POST, '修改');
                    if (count($arrPostKey) > 0) {
                        $int = null;
                        foreach ($arrPostKey as $strTemp) {
                            if (substr($strTemp, -7) == '_update') {
                                $int = str_replace('_update', '', $strTemp);
                                break;
                            }
                        }
                        if (!is_null($int)) {
                            if (!$this->mdlAcl->checkPermission($this->arrManagerData['group_name'], 'sys_config', 'update')) {
                                Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin', '你无权修改!');
                            }
                            $arrTemp = array(
                                'id' => isset($_POST[$int . '_id']) ? $_POST[$int . '_id'] : '',
                                'name' => isset($_POST[$int . '_name']) ? $_POST[$int . '_name'] : '',
                                'val' => isset($_POST[$int . '_val']) ? $_POST[$int . '_val'] : '',
                                'desed' => isset($_POST[$int . '_desed']) ? 1 : 0,
                            );
                            $arrData = $mdlSys->get($arrTemp['id']);
                            if (!$arrData) {
                                $arrAssign['strMsg'] = '参数错误,请检查!';
                            } else {
                                unset($arrTemp['id']);
                                if (!$arrData['desed'] && $arrTemp['desed']) {
                                    $arrTemp['val'] = $mdlSys->getDesed($arrTemp['val']);
                                } elseif ($arrData['desed'] && $arrTemp['desed']) {
                                    if ($mdlSys->getUnDesed($arrTemp['val']) == $arrData['val']) {
                                        unset($arrTemp['val']);
                                    } else {
                                        $arrTemp['val'] = $mdlSys->getDesed($arrTemp['val']);
                                    }
                                }
                                if ($mdlSys->update($arrTemp, $arrData['id'])) {
                                    $arrAssign['strOk'] = '修改成功!';
                                    $arrAssign['arrSys'] = $mdlSys->select();
                                } else {
                                    $arrAssign['strMsg'] = '未能完成修改,请检查!';
                                }
                            }
                        } else {
                            $arrAssign['strMsg'] = '参数错误!';
                        }
                    } else {
                        $arrPostKey = array_keys($_POST, '删除');
                        if (count($arrPostKey) > 0) {
                            $int = null;
                            foreach ($arrPostKey as $strTemp) {
                                if (substr($strTemp, -7) == '_delete') {
                                    $int = str_replace('_delete', '', $strTemp);
                                    break;
                                }
                            }
                            if (!is_null($int)) {
                                if (!$this->mdlAcl->checkPermission($this->arrManagerData['group_name'], 'sys_config', 'delete')) {
                                    Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin', '你无权删除!');
                                }
                                $arrData = isset($_POST[$int . '_id']) ? $mdlSys->get($_POST[$int . '_id']) : false;
                                if (!$arrData) {
                                    $arrAssign['strMsg'] = '参数错误,请检查!';
                                } elseif ($mdlSys->delete($arrData['id'])) {
                                    $arrAssign['strOk'] = '删除成功!';
                                    $arrAssign['arrSys'] = $mdlSys->select();
                                } else {
                                    $arrAssign['strMsg'] = '删除失败,请检查!';
                                }
                            } else {
                                $arrAssign['strMsg'] = '参数错误!';
                            }
                        }
                    }
                }
            }
        }
        $this->_display('admin/index.php', $arrAssign);
    }
    
    
}